package com.yuanfeng.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.activity.dto.NoneGoodsUpdateDTO;
import com.yuanfeng.activity.entity.ActivityNoneGoodsEntity;
import com.yuanfeng.activity.entity.ActivityNoneTypeEntity;
import com.yuanfeng.activity.mapper.ActivityNoneGoodsMapper;
import com.yuanfeng.activity.mapper.ActivityNoneTypeMapper;
import com.yuanfeng.activity.service.ActivityNoneGoodsService;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.EasyPoiUtils;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.goods.GoodsRecResultDetailVO;
import com.yuanfeng.commoms.vo.goods.NoneGoodsQueryDTO;
import com.yuanfeng.commoms.vo.goods.VoucherMessageVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ActivityNoneGoodsServiceImpl extends ServiceImpl<ActivityNoneGoodsMapper, ActivityNoneGoodsEntity>
    implements ActivityNoneGoodsService{

    @Autowired
    private ActivityNoneTypeMapper activityNoneTypeMapper;
    @Override
    public ResponseResult deleteNoneGoods(NoneGoodsUpdateDTO param) {
        this.baseMapper.delete(new LambdaQueryWrapper<ActivityNoneGoodsEntity>()
                       .in(ActivityNoneGoodsEntity::getCommonId,param.getCommonIdList()));
        return ResponseResult.success();
    }

    @Override
    public ResponseResult updateActivityNoneGoods(NoneGoodsUpdateDTO param) {
        Integer noneType = param.getNoneType();
        String activityVerifyRemark = param.getActivityVerifyRemark();
        Integer displayOrder = null == param.getDisplayOrder()?0:param.getDisplayOrder();
        List<NoneGoodsUpdateDTO> noneBatchs = Lists.newArrayList();

        if (param.getOpType().equals(1)) {

            Boolean isExist = this.baseMapper.checkGoodsIsExist(param);
            if (isExist) {
                return ResponseResult.fail(0, "商品已经存在，请不要重复添加");
            } else {

                param.getCommonIdList().forEach(commonId -> {
                    NoneGoodsUpdateDTO noneBatch = new NoneGoodsUpdateDTO();

                    noneBatch.setNoneType(noneType);
                    noneBatch.setCommonId(commonId);
                    noneBatch.setActivityVerifyRemark(activityVerifyRemark);
                    noneBatch.setDisplayOrder(displayOrder);
                    noneBatchs.add(noneBatch);
                });
                this.baseMapper.insertNoneGoodsBatch(noneBatchs);

            }

        } else {
            if (param.getNoneGoodsIdList().size()<=0){
                return ResponseResult.fail(0, "缺少必要参数noneGoodsIdList");
            }
            this.baseMapper.updateNoneGoodsBatch(param);
        }
        if (null != param.getCornerId()) {
            this.baseMapper.updateCornerBatch(param);
        }

        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryNoneGoods(NoneGoodsQueryDTO param) throws IllegalAccessException {

        // 权限判断
        UserInfoVO userInfoVO = ParseTokenUtils.getPlatformUser(param.getToken());

        if (userInfoVO != null) {
            Map data = ParseMapUtils.beanToMap(param);
            IPage<GoodsRecResultDetailVO> page = this.baseMapper.selectNoneGoods(new Query<GoodsRecResultDetailVO>().getPage(data),
                    param);
            if (!CollectionUtils.isEmpty(page.getRecords())) {
                for (GoodsRecResultDetailVO good : page.getRecords()) {
                    List<VoucherMessageVO> voucherMes = this.baseMapper.selectVoucherMes(good.getCommonId());
                    if (CollectionUtils.isEmpty(voucherMes)) {
                        voucherMes = Lists.newArrayList();
                    }
                    good.setVoucherMes(voucherMes);
                }
            }
            return ResponseResult.success(PageUtils.getPage(page));
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
    }

    @Override
    public ResponseResult queryActivityGoods(NoneGoodsQueryDTO param) throws IllegalAccessException {

        // 权限判断
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());

        if (userInfoVO != null) {
            Integer shopId = Integer.valueOf(userInfoVO.getShopId());
            param.setShopId(shopId);
            Map data = ParseMapUtils.beanToMap(param);
            IPage<GoodsRecResultDetailVO> page = this.baseMapper.selectNoneGoods(new Query<GoodsRecResultDetailVO>().getPage(data),
                    param);
            if (!CollectionUtils.isEmpty(page.getRecords())) {
                for (GoodsRecResultDetailVO good : page.getRecords()) {
                    List<VoucherMessageVO> voucherMes = this.baseMapper.selectVoucherMes(good.getCommonId());
                    if (CollectionUtils.isEmpty(voucherMes)) {
                        voucherMes = Lists.newArrayList();
                    }
                    good.setVoucherMes(voucherMes);
                }
            }
            return ResponseResult.success(PageUtils.getPage(page));
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
    }

    @Override
    public ResponseResult exportNoneGoods(NoneGoodsQueryDTO param, HttpServletResponse response) throws IllegalAccessException {

        Map data= ParseMapUtils.beanToMap(param);
        IPage<GoodsRecResultDetailVO> page=this.baseMapper.selectNoneGoods(new Query<GoodsRecResultDetailVO>().getPage(data),
                param);

        if (CollectionUtils.isEmpty(page.getRecords())) {
            return ResponseResult.fail(BizCodeEnume.NO_INFORMATION_OBTAINED);
        }

        for (GoodsRecResultDetailVO good : page.getRecords()) {
            List<VoucherMessageVO> voucherMes = this.baseMapper.selectVoucherMes(good.getCommonId());
            if (CollectionUtils.isEmpty(voucherMes)) {
                voucherMes = Lists.newArrayList();
            }
            good.setVoucherMes(voucherMes);
        }

        // 导出操作
        String fileName = EasyPoiUtils.exportExcel(page.getRecords(), "活动专区商品", "活动专区商品", GoodsRecResultDetailVO.class,
                System.currentTimeMillis() + "", true, response);

        return ResponseResult.success(fileName);
    }

    @Override
    public ResponseResult deleteNoneType(Map<String, Object> param) {
        activityNoneTypeMapper.delete(new LambdaQueryWrapper<ActivityNoneTypeEntity>()
                              .eq(ActivityNoneTypeEntity::getNoneType,Integer.valueOf(param.get("noneType").toString())));

       this.baseMapper.delete(new LambdaQueryWrapper<ActivityNoneGoodsEntity>()
               .eq(ActivityNoneGoodsEntity::getNoneType,Integer.valueOf(param.get("noneType").toString())));
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }
}




